<?php
/**
 * @desc 订单数据层
 * @author kevin.wang
 * @date 2015-01-01 12:00:00
 */
class Order extends Base
{
	//数据表
	protected $table = 'pert_order';
	protected $dtable = 'pert_order_detail';
	protected $gtable = 'pert_goods';
	//私人订制列表
	public function getOrderList(array $con)  
    {
		 $table = $this->table;
		 $dtable = $this->dtable;
		 $gtable = $this->gtable;
		 $filed = "a.`order_id`,a.`order_sn`,a.`user_id`,a.`total_amount`,a.`order_status`,a.`pay_status`,a.`order_date`,a.`order_expire`,a.`travel_date`,c.`goods_name`,b.`sell_num`,b.`goods_sn`,c.`image`,a.`raw_add_time`,a.`is_travel`";

		 $sql = "select {$filed} from `{$table}` as a left join `{$dtable}` as b on a.`order_sn` = b.`order_sn` left join `{$gtable}` as c on b.`goods_sn` = c.`goods_sn` where a.`delete_flag` = 0 ";
		 $where = "";
		 if(!empty($con['goods_name'])){
			 $where .= " and c.`goods_name` like '%{$con['goods_name']}%' ";
		 }
		 if(!empty($con['pay_status'])){
			 if($con['pay_status'] == 5){
				 $where .= " and a.`is_travel` = '1' ";
			 }else{
				 $con['pay_status'] -= 1;
				 $where .= " and a.`pay_status` = '{$con['pay_status']}' ";
			 }
		 }
		 if(!empty($con['travel_date'])){
			 $where .= " and a.`travel_date` like '%{$con['travel_date']}%' ";
		 }
		 if(!empty($con['order_date'])){
			 $where .= " and a.`raw_add_time` like '%{$con['order_date']}%' ";
		 }
		 $group = " group by  a.`order_sn`";
		 $return = $this->select_table($sql.$where.$group, array());

		 $sql = "select count(a.`order_id`) as num from `{$table}` as a left join `{$dtable}` as b on a.`order_sn` = b.`order_sn` left join `{$gtable}` as c on b.`goods_sn` = c.`goods_sn` where a.`delete_flag` = 0 ";
		 $return[] = $this->select_table($sql.$where, array(), Base::QUERY_COLUMN);
		 return $return;
    }
	//删除订单
	public function deleteOrder($ids){
		$table = $this->table;
		$dtable = $this->dtable;
		//$sql = "update `{$table}` set `delete_flag` = 1 where `order_id` in ({$ids})";
		$sql = "update `{$table}` as a,`{$dtable}` as b set a.`delete_flag` = 1,b.`delete_flag` = 1 where a.`order_sn` = b.`order_sn` and a.`order_id` in ({$ids})";
		return $this->update_table($sql, array());
	}
	//写入订单
	public function insertOrder(array $data){
		if(empty($data)){
			return false;
		}
		return self::insert_table($data, $this->table);
	}
	//获取订单
	public function getOrder($id){
		$table = $this->table;
		$dtable = $this->dtable;
		$filed = "`order_id`,`order_sn`,`user_id`,`total_amount`,`order_status`,`pay_status`,`is_travel`,`order_date`,`order_expire`,`travel_date`,`raw_add_time`";
		$sql = "select {$filed} from `{$table}` where `order_id` = :order_id and `delete_flag` = 0";
		$return['mainOrder'] = $this->select_table($sql, array(':order_id'=>$id), Base::QUERY_RAW);
		$filed = "`order_detail_id`,`order_sn`,`order_detail_sn`,`goods_name`,`goods_sn`,`sell_num`,`price`,`total_price`,`detail_status`,`pay_status`,`raw_add_time`";
		$sql = "select {$filed} from `{$dtable}` where `order_sn` = :order_sn and `delete_flag` = 0";
		$return['childOrder'] = $this->select_table($sql, array(':order_sn'=>$return['mainOrder']['order_sn']));
		return $return;
	}
	//修改主订单
	public function editMainOrder($id, array $data){
		if(empty($id) || empty($data)){
			return false;
		}
		$table = $this->table;
		$sql = "update `{$table}` set ";
		$binds = array();
		foreach($data as $key=>$value){
			$sql .= "`{$key}` = :{$key},";
			$binds[":{$key}"] = $value;
		}
		$sql = trim($sql, ',');
		$sql .= " where `order_id` = :order_id";
		$binds[':order_id'] = $id;
		return $this->update_table($sql, $binds);
	}
	//修改子订单
	public function editChildOrder($id, array $data){
		if(empty($id) || empty($data)){
			return false;
		}
		$dtable = $this->dtable;
		$sql = "update `{$dtable}` set ";
		$binds = array();
		foreach($data as $key=>$value){
			$sql .= "`{$key}` = :{$key},";
			$binds[":{$key}"] = $value;
		}
		$sql = trim($sql, ',');
		$sql .= " where `order_detail_id` = :order_detail_id";
		$binds[':order_detail_id'] = $id;
		return $this->update_table($sql, $binds);
	}
}
